<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2019/3/25
 * Time: 11:30
 */
namespace App\Services\RepaymentPay;

class LargePayXService
{
    protected $apiUrl;
    protected $mchId;
    protected $appId;
    protected $apiKey;
    protected $tradeNotifyUrl;
    protected $withdrawNotifyUrl;
    protected $withdrawHandleNotifyUrl;

    public function __construct()
    {
        $this->apiUrl = config('pay.fourpay.api_url');
        $this->mchId = config('pay.fourpay.mch_id');
        $this->appId = config('pay.fourpay.app_id');
        $this->apiKey = config('pay.fourpay.api_key');
        $this->tradeNotifyUrl = config('pay.fourpay.large_pay_x.trade_notify_url');
        $this->withdrawNotifyUrl = config('pay.fourpay.large_pay_x.withdraw_notify_url');
        $this->withdrawHandleNotifyUrl = config('pay.fourpay.large_pay_x.withdraw_handle_notify_url');
    }

    /**
     * 获取接口请求结果
     * @param $param
     * @param $url
     * @return mixed
     */
    protected function getRequestResult($param, $url)
    {
        $param['sign'] = getSign($param, $this->apiKey);
        $client = new \GuzzleHttp\Client(['verify' => false]);
        $response = $client->request('post', $url, ['form_params' => $param]);
        $json = $response->getBody()->getContents();
        $result = json_decode($json, true);
        return [
            'params' => $param,
            'response' => $result
        ];
    }

    /**
     * 注册子商户
     * @param $tradeNo
     * @param $realname
     * @param $idCard
     * @return mixed
     */
    public function subMerchantRegister($tradeNo, $realname, $idCard)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['realname'] = $realname;
        $param['id_card'] = $idCard;
        $url = $this->apiUrl . 'api/pay/large_pay_x/sub_merchant_register';
        return $this->getRequestResult($param, $url);
    }

    /**
     * 子商户信息查询
     * @param $tradeNo
     * @param $subMchNo
     * @return mixed
     */
    public function subMerchantQuery($tradeNo, $subMchNo)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['sub_mch_no'] = $subMchNo;
        $url = $this->apiUrl . 'api/pay/large_pay_x/sub_merchant_query';
        return $this->getRequestResult($param, $url);
    }

    /**
     * 订单查询
     * @param $tradeNo
     * @param $subMchNo
     * @param $queryType
     * @return mixed
     */
    public function query($tradeNo, $subMchNo, $queryType)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['sub_mch_no'] = $subMchNo;
        $param['query_type'] = $queryType;//trade交易 withdraw提现
        $url = $this->apiUrl . 'api/pay/large_pay_x/query';
        return $this->getRequestResult($param, $url);
    }

    /**
     * 子商户签约
     * @param $tradeNo
     * @param $subMchNo
     * @param $bankCardNo
     * @param $bankCardMobile
     * @param $expireDate
     * @param $cvn2
     * @return mixed
     */
    public function subMerchantSign($tradeNo, $subMchNo, $bankCardNo, $bankCardMobile, $expireDate, $cvn2)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['sub_mch_no'] = $subMchNo;
        $param['bank_card_no'] = $bankCardNo;
        $param['bank_card_mobile'] = $bankCardMobile;
        $param['expire_date'] = $expireDate;
        $param['cvn2'] = $cvn2;
        $url = $this->apiUrl . 'api/pay/large_pay_x/sub_merchant_sign';
        return $this->getRequestResult($param, $url);
    }

    /**
     * 子商户签约确认
     * @param $tradeNo
     * @param $subMchNo
     * @param $bankCardNo
     * @param $smsCode
     * @return mixed
     */
    public function subMerchantSignConfirm($tradeNo, $subMchNo, $bankCardNo, $smsCode)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['sub_mch_no'] = $subMchNo;
        $param['bank_card_no'] = $bankCardNo;
        $param['code'] = $smsCode;
        $url = $this->apiUrl . 'api/pay/large_pay_x/sub_merchant_sign_confirm';
        return $this->getRequestResult($param, $url);
    }

    /**
     * 交易
     * @param $tradeNo
     * @param $subMchNo
     * @param $bankCode
     * @param $bankCardNo
     * @param $bankCardMobile
     * @param $expireDate
     * @param $cvn2
     * @param $amount
     * @param $rate
     * @param $singlePrice
     * @param $address
     * @return mixed
     */
    public function trade($tradeNo, $subMchNo, $bankCode, $bankCardNo, $bankCardMobile, $expireDate, $cvn2,
                          $amount, $rate, $singlePrice, $address)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['sub_mch_no'] = $subMchNo;
        $param['bank_code'] = $bankCode;
        $param['bank_card_no'] = $bankCardNo;
        $param['bank_card_mobile'] = $bankCardMobile;
        $param['expire_date'] = $expireDate;
        $param['cvn2'] = $cvn2;
        $param['amount'] = (string)round($amount * 100, 2);
        $param['sub_mch_rate'] = $rate;
        $param['single_price'] = (string)round($singlePrice * 100, 2);
        $param['address'] = $address;
        $param['notify_url'] = $this->tradeNotifyUrl;
        $param['mcc'] = '';
        $param['merchant_optional_id'] = '';
        $url = $this->apiUrl . 'api/pay/large_pay_x/trade';
        return $this->getRequestResult($param, $url);
    }

    /**
     * 提现
     * @param $tradeNo
     * @param $subMchNo
     * @param $idCard
     * @param $bankCode
     * @param $bankCardNo
     * @param $amount
     * @param $rate
     * @param $singlePrice
     * @return mixed
     */
    public function withdraw($tradeNo, $subMchNo, $idCard, $bankCode, $bankCardNo, $amount, $rate, $singlePrice)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['sub_mch_no'] = $subMchNo;
        $param['id_card'] = $idCard;
        $param['bank_code'] = $bankCode;
        $param['bank_card_no'] = $bankCardNo;
        $param['amount'] = (string)round($amount, 2);
        $param['sub_mch_rate'] = $rate;
        $param['single_price'] = (string)round($singlePrice, 2);
        $param['notify_url'] = $this->withdrawNotifyUrl;
        $url = $this->apiUrl . 'api/pay/large_pay_x/withdraw';
        return $this->getRequestResult($param, $url);
    }

    /**
     * 提现
     * @param $tradeNo
     * @param $subMchNo
     * @param $idCard
     * @param $bankCode
     * @param $bankCardNo
     * @param $amount
     * @param $rate
     * @param $singlePrice
     * @return mixed
     */
    public function withdrawHandle($tradeNo, $subMchNo, $idCard, $bankCode, $bankCardNo, $amount, $rate, $singlePrice)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['sub_mch_no'] = $subMchNo;
        $param['id_card'] = $idCard;
        $param['bank_code'] = $bankCode;
        $param['bank_card_no'] = $bankCardNo;
        $param['amount'] = (string)round($amount, 2);
        $param['sub_mch_rate'] = $rate;
        $param['single_price'] = (string)round($singlePrice, 2);
        $param['notify_url'] = $this->withdrawHandleNotifyUrl;
        $url = $this->apiUrl . 'api/pay/large_pay_x/withdraw';
        return $this->getRequestResult($param, $url);
    }
}
